<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>提交新闻</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        h2 {
            margin-bottom: 10px;
        }
        form {
            margin-bottom: 20px;
        }
        label {
            font-weight: bold;
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"],
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-bottom: 10px;
        }
        input[type="button"] {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 5px;
        }
        input[type="button"]:hover {
            background-color: #45a049;
        }
        #newsList {
            margin-top: 20px;
        }
        p {
            margin-bottom: 5px;
        }
    </style>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script>
        function submitNews() {
            var title = document.getElementById("title").value;
            var content = document.getElementById("content").value;

            if (title.trim() === '' || content.trim() === '') {
                alert("标题和内容不能为空！");
                return;
            }

            var data = {
                title: title,
                content: content
            };

            fetch("/submit-news", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(data)
            })
                .then(response => response.text())
                .then(newsId => {
                    // 清空表单字段
                    document.getElementById("title").value = "";
                    document.getElementById("content").value = "";

                    // 更新页面中已提交的新闻列表
                    var newsList = document.getElementById("newsList");
                    newsList.innerHTML += "<p><strong>" + title + "</strong>: " + content + "</p>";
                });
        }
    </script>
</head>
<body>
<h2>提交新闻</h2>
<form id="newsForm">
    <label for="title">标题：</label>
    <input type="text" id="title" name="title">
    <label for="content">内容：</label>
    <textarea id="content" name="content" rows="4"></textarea>
    <input type="button" value="提交" onclick="submitNews()">
</form>
<h2>已提交的新闻</h2>
<div id="newsList"></div>
</body>
</html>